package com.smartlbs.controller;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.util.StringUtils;
import com.smartlbs.baseframe.utils.VolidateCodeUtil;
import com.smartlbs.baseframe.utils.VolidateCodeUtil.Validate;
import com.smartlbs.baseframe.web.ResponseInfo;
import com.smartlbs.entity.SysUser;
import com.smartlbs.service.ISysUserService;

/**
 * 管理用户控制层实现类
 *
 */
@Controller
@RequestMapping("adminuser")
public class SysUserController {
	@Autowired
	private ISysUserService iTblSysUserService;
	
	@RequestMapping("login")
	@ResponseBody
	public ResponseInfo login(HttpServletRequest request,HttpServletResponse  response){
		
		ResponseInfo info = new ResponseInfo();
		int result = 0;
		String message = "登录失败";
		Object data = null;
		String name= (String) request.getParameter("username");
		String password = (String)request.getParameter("password");
		String code = request.getParameter("code").toLowerCase();
		try {
			//验证码 验证
			String volicode = (String)request.getSession().getAttribute("code");
			if(StringUtils.isEmpty(code)){
				message="验证码不可为空";
				result=-3;
				info.setMessage(message);
				info.setResult(result);
				return info;
			}
			if(!code.equals(volicode)){
				message="验证码错误";
				result=-2;
				info.setMessage(message);
				info.setResult(result);
				return info;
			}
				SysUser login = iTblSysUserService.login(name, password);
				if(login !=null ){
					HttpSession session = request.getSession();
					String id = session.getId();
					request.getSession().setAttribute("sysUser", login);
					Map<String, String> map=new HashMap<>();
					map.put("u_token", id);
					data=map;
					result=1;
					message="登录成功";
				}else {
					message="密码错误";
					result=-1;
				}
		} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		message = "发生异常";
		result=-2;
		
		}
		info.setData(data);
		info.setMessage(message);
		info.setResult(result);
		return info;
	}
	@RequestMapping("insert")
	@ResponseBody
	public ResponseInfo insert(String name,String password){
		ResponseInfo info = new ResponseInfo();
		try {
			info = iTblSysUserService.insert(name, password);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			info.setMessage("注册失败");
			
		}
		
		return info;
	}

	@ResponseBody
	@RequestMapping("getcode")
	public String getValidate(HttpServletRequest request){
		HttpSession session = request.getSession();
		Validate validate = VolidateCodeUtil.getRandomCode();
		String code = validate.getValue();
		session.setAttribute("code", code.toLowerCase());
		String base64Str = validate.getBase64Str();
		return "data:image/jpeg;base64,"+base64Str;
	}
	
}
